package org.mijia.dotsbox.rcp;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
import org.mijia.dotsbox.DotsboxAppGui;

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {

    public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
        super(configurer);
    }

    public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) {
        return new ApplicationActionBarAdvisor(configurer);
    }
    
    public void preWindowOpen() {
    	IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
		configurer.setInitialSize(new Point(300, 550));
        configurer.setShowCoolBar(false);
        configurer.setShowStatusLine(false);
        configurer.setShowProgressIndicator(false);
        configurer.setTitle("DotsBox: Connecting the Dots");
    }
    
	
	@Override
    public void postWindowCreate() {
        super.postWindowCreate();
        IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
        Shell windowShell = configurer.getWindow().getShell();
        DotsboxAppGui.getInstance().initInstance(windowShell);
    }
	
    @Override
    public void postWindowOpen() {
        super.postWindowOpen();
        DotsboxAppGui.getInstance().setStatusMessage("Ready");
    }
    
    // create windon custom content
    @Override
    public void createWindowContents(Shell shell) {
        Menu menuBar = getWindowConfigurer().createMenuBar();
        shell.setMenuBar(menuBar);

        GridLayout shellLayout = new GridLayout();
        shellLayout.marginWidth = 0;
        shellLayout.horizontalSpacing = 0;
        shellLayout.marginHeight = 0;
        shellLayout.verticalSpacing = 0;
        shellLayout.numColumns = 1;
        shell.setLayout(shellLayout);

        ViewForm headerBar = new ViewForm(shell, SWT.BORDER | SWT.FLAT);
        headerBar.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true,
                false));
        headerBar.setOnlyBorderTopBottom(true);
        headerBar.setToolBarMode(true);
        headerBar.setBackground(headerBar.grayViewFormColor);
        createHeaderBar(headerBar);
        
        Control pageComposite = getWindowConfigurer()
                .createPageComposite(shell);
        pageComposite.setLayoutData(new GridData(GridData.FILL, GridData.FILL,
                true, true));

        Control statusLine = getWindowConfigurer().createStatusLineControl(shell);
        statusLine.setLayoutData(new GridData(GridData.FILL, GridData.CENTER,
                true, false));
        DotsboxAppGui.getInstance().setStatusLineManager(
                getWindowConfigurer().getActionBarConfigurer()
                        .getStatusLineManager());
        
        headerBar.layout();
        headerBar.redraw();
        headerBar.update();
        shell.layout(true);
    }

	private void createHeaderBar(ViewForm parent) {
        Composite bottomContainer = new Composite(parent, SWT.NONE);
        bottomContainer.setBackground(parent.grayViewFormColor);
        bottomContainer.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
        parent.setContent(bottomContainer, true);

        GridLayout bottomContainerLayout = new GridLayout(1, true);
        bottomContainerLayout.marginWidth = 5;
        bottomContainerLayout.marginHeight = 3;
        bottomContainerLayout.marginTop = 1;
        bottomContainer.setLayout(bottomContainerLayout);
        
        Composite searchBar = new Composite(bottomContainer, SWT.NONE);
        GridLayout layout = new GridLayout(2, false);
        layout.marginWidth = 2;
        layout.marginHeight = 2;
        searchBar.setLayout(layout);
        searchBar.setBackground(parent.grayViewFormColor);
        searchBar.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        
        final Text searchText = new Text(searchBar, SWT.FLAT);
        searchText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        
        ToolBar searchToolbar = new ToolBar(searchBar, SWT.FLAT | SWT.NO_FOCUS);
        searchToolbar.setBackground(parent.grayViewFormColor);
        searchToolbar.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));

        final ToolItem searchTool = new ToolItem(searchToolbar, SWT.PUSH);
        searchTool.setImage(DotsboxAppGui.getImage("icons/search.png"));
        searchTool.setToolTipText("Search Dotsbox");
        // TODO: searchTool.push
        
        final ToolItem sepTool = new ToolItem(searchToolbar, SWT.SEPARATOR | SWT.VERTICAL);
        
        final ToolItem homeTool = new ToolItem(searchToolbar, SWT.PUSH);
        homeTool.setImage(DotsboxAppGui.getImage("icons/dotsbox_24.png"));
        homeTool.setToolTipText("Open DotsBox Page");
        homeTool.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				DotsboxAppGui.getInstance().openDotsBoxHomepage();
			}
        });
                
        final Color borderColor = parent.viewFormBorderMiddleColor;
        searchBar.addPaintListener(new PaintListener() {
            public void paintControl(PaintEvent e) {
                GC gc = e.gc;
                gc.setForeground(searchText.getBackground());
                Rectangle b = searchText.getBounds();
                gc.drawRectangle(b.x - 1, b.y - 1, b.width + 1, b.height + 1);
                
                gc.setForeground(borderColor);
                gc.drawRectangle(b.x - 2, b.y - 2, b.width + 3, b.height + 3);
            }
        });
        
        searchText.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent event) {
                String keyword = searchText.getText().trim();
                if(keyword.length() != 0 && event.character == SWT.CR) {
                    System.out.println(keyword);
                }
            }
        });
	}
}
